package com.gpoint.rdp.door.home.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.BeanUtils;
import com.gpoint.rdp.base.core.util.time.DateUtil;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.door.logs.persistence.entity.SwipeRecordPo;
import com.gpoint.rdp.door.logs.service.SwipeRecordQueryService;
import com.gpoint.rdp.door.zoneCTL.persistence.entity.DoorControllerPo;
import com.gpoint.rdp.door.zoneCTL.service.DoorControllerQueryService;

/**
 * TB_DOOR_SWIPERECORD 控制类
 *
 * <pre>
 *  
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-30 18:57:54
 * </pre>
 */
@Controller
@RequestMapping("/rdp/door/home/")
public class DoorHomeController extends GenericController {
	@Resource
	private SwipeRecordQueryService swipeRecordQueryService;
	
	@Resource
	private DoorControllerQueryService doorControllerQueryService;
	

	@RequestMapping("index")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView("/rdp/home/homeService.jsp");
		List<SwipeRecordPo> pos = swipeRecordQueryService.queryDayCount();
		Map<String,Integer> countMap = getMounthCount(pos);
		int monthCount =  countMap.get("monthCount");
		int nowCount =  countMap.get("nowCount");
		String json = doorControllerQueryService.findAll();
		List<DoorControllerPo> list = DoorControllerPo.fromJsonArrayString(json);
		int deviceCount = BeanUtils.isEmpty(list)?0:list.size();
        mv.addObject("dayCountList", pos);
        mv.addObject("monthCount", monthCount);
        mv.addObject("nowCount", nowCount);
        mv.addObject("deviceCount", deviceCount);
		return mv;

	}

	private Map<String,Integer> getMounthCount(List<SwipeRecordPo> pos) {
		int monthCount = 0;
		int nowCount = 0;
		Map<String,Integer> map = new HashMap<String,Integer>(256);
		map.put("monthCount", monthCount);
		map.put("nowCount", nowCount);
		if (BeanUtils.isEmpty(pos)) {
			return map;
		}
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		for (SwipeRecordPo po : pos) {
			monthCount += po.getPassCount();
			if(now.equals(po.getDateStr())) {
				nowCount = po.getPassCount();
			}
		}
		
		if(nowCount==0) {
			SwipeRecordPo r = new SwipeRecordPo();
			r.setDateStr(now);
			r.setPassCount(nowCount);
			pos.add(r);
		}
		map.put("monthCount", monthCount);
		map.put("nowCount", nowCount);
		return map;
	}
	
	
}